import { _decorator, Component, Label, math, Node, Tween, tween, Vec3 } from 'cc';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
import { NodeUtil } from '../AshFramework/Utils/NodeUtil';
import PrefsUtil from '../AshFramework/Utils/PrefsUtil';
import { Constant } from '../AshFramework/Datas/Constant';
import { Panel, UIManager } from '../AshFramework/Managers/UIManager';
import Banner from '../Banner';
import UI_BreatheAni from '../AshFramework/UI/UI_BreatheAni';
import { GameManager } from '../GameManager';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { PieceItem } from './PieceItem';
import { PoolManager } from '../AshFramework/Managers/PoolManager';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';

const { ccclass, property } = _decorator;

@ccclass('UnpackPanel')
export class UnpackPanel extends Component {
    Panel: Node;
    GetPanel: Node;
    Package: Node;
    CardContent: Node;
    GetPanelMoney: Node;
    GetPanelMoneyLabel: Label;
    GetButton: Node;

    pieceItems: Node[] = [];

    protected onLoad(): void {
        this.Panel = NodeUtil.GetNode("Panel", this.node);
        this.GetPanel = NodeUtil.GetNode("GetPanel", this.node);
        this.Package = NodeUtil.GetNode("Package", this.node);
        this.CardContent = NodeUtil.GetNode("CardContent", this.node);
        this.GetPanelMoney = NodeUtil.GetNode("GetPanelMoney", this.node);
        this.GetPanelMoneyLabel = NodeUtil.GetComponent("GetPanelMoneyLabel", this.node, Label);
        this.GetButton = NodeUtil.GetNode("GetButton", this.node);
    }

    quality: number = 0;
    isShaking: boolean = false;

    Show() {
        this.pieceItems.forEach(e => PoolManager.PutNode(e));
        this.pieceItems = [];

        this.isShaking = false;
        this.Panel.active = true;
        this.GetPanel.active = false;

        let value = math.random();
        this.quality = 0;

        if (ZTool.InRange(value, 0.7, 0.95)) this.quality = 1;
        if (ZTool.InRange(value, 0.95, 1)) this.quality = 2;

        this.Package.setScale(2, 2, 2);
        this.Package.getComponent(UI_BreatheAni).Set();
        this.Package.children.forEach(e => e.active = e.name == `${this.quality}`);
    }

    ShowGetPanel() {
        let datas;

        if (this.quality == 2) {
            datas = GameManager.Instance.GetOwnerItems().sort((a, b) => b.Quality - a.Quality);
        } else {
            datas = ZTool.Shuffle(GameManager.Instance.GetOwnerItems());
        }

        let length = 0;
        length = ZTool.Clamp(datas.length, 0, 6);
        for (let i = 0; i < length; i++) {
            const data = datas[i];
            //5-([0-3] + 1)
            let count = Math.floor((5 - (data.Quality + 1)) * (math.random() + 0.01) * 10) * (this.quality + 1);
            data.Piece += count;
            PoolManager.GetNode("Prefabs/UI/PieceItem", this.CardContent, Vec3.ZERO).then((item: Node) => {
                item.getComponent(PieceItem).Init(data, count, i * 0.1);
                this.pieceItems.push(item);
            });
        }

        let money = this.quality == 0 ? 100 : 100 * this.quality * 5;
        this.GetPanelMoneyLabel.string = `X${money}`;
        GameManager.Instance.Money += money;
        EventManager.Scene.emit(MyEvent.REFRESH_SHOPITEMS);

        this.scheduleOnce(() => this.GetPanelMoney.active = true, length * 0.1 + 0.5);
        this.scheduleOnce(() => this.GetButton.active = true, length * 0.1 + 1);
    }

    OnButtonClick(event, data) {
        AudioManager.PlaySound(Audios.ButtonClick);

        switch (event.target.name) {
            case 'Mask':
                if (this.isShaking) return;
                this.isShaking = true;
                NodeUtil.Shaking(this.Package, 50, 0.01, 50, () => {
                    tween(this.Package).to(0.2, { scale: Vec3.ZERO }).call(() => {
                        this.Panel.active = false;
                        this.GetPanel.active = true;
                        this.GetPanel.setScale(0, 0, 0);
                        this.GetPanelMoney.active = false;
                        this.GetButton.active = false;

                        tween(this.GetPanel).to(0.3, { scale: Vec3.ONE }, { easing: `circIn` }).call(() => {
                            this.ShowGetPanel();
                        }).start();

                    }).start();
                });
                break;
            case 'GetButton':
                UIManager.GetInstance().HidePanel(Panel.UnpackPanel);
                break;
        }
    }

}